package com.chenl.provider.DEMOS.WebService.参考;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.chenl.provider.DEMOS.WebService.参考.GetWSByAxis2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
@Slf4j
public class webServiceController {
    private static final String url="http://localhost:8082/services/api?wsdl";
    private static final String url2="https://10.190.7.89:7901/WSMBService";
    private static final String url3="https://10.190.68.203:7901/WSMBService?wsdl";
    static final String param="<?xml version=\"1.0\" encoding=\"GB2312\"?>  \n" +
            "    <Msg>   \n" +
            "       <Head>     \n" +
            "\t<Id>MES20230308083300</Id>     \n" +
            "\t<Name>库容查询</Name>       \n" +
            "\t<Source>ZNWL</Source>     \n" +
            "\t<Target>PKWMS</Target>     \n" +
            "\t<SerName>ProductRealtimeInventoryZNWL</SerName>       \n" +
            "\t<MsgType>0</MsgType>     \n" +
            "\t<RtCode/>     \n" +
            "\t<RtDesc/>     \n" +
            "\t<Backup1/>     \n" +
            "\t<Backup2/>     \n" +
            "\t<Backup3/>     \n" +
            "\t<Backup4/>       \n" +
            "\t<Backup5/>     \n" +
            "\t<Date>2023/03/08 08:33:00</Date>     \n" +
            "\t<Attrs>       \n" +
            "\t    <Attr/>       \n" +
            "\t    <Attr></Attr>     \n" +
            "\t</Attrs>   \n" +
            "       </Head>   \n" +
            "    </Msg>";
    public static void main(String[] args) throws Exception {
//        String ws_zt_bas = sendWebService(url, "Ws_zt_bas", param);
//        String ws_zt_bas = sendWebService2(url, "http://BasicDataPullApi.API.WebService.DEMOS.provider.chenl.com/","Ws_zt_bas", param);
//        String ws_zt_bas = sendWebService2(url3, "http://tempuri.org/ZY_WS_MB_MSP","ws_mb", param);
        String s = sendWstXml("<?xml version=\"1.0\" encoding=\"GB2312\"?>  \n" +
                "<Msg>   \n" +
                "<Head>    \n" +
                " <Id>MES20230308083300</Id>     \n" +
                "<Name>库容查询</Name>       \n" +
                "<Source>ZNWL</Source>     \n" +
                "<Target>PKWMS</Target>     \n" +
                "<SerName>ProductRealtimeInventoryZNWL</SerName>       \n" +
                "<MsgType>0</MsgType>     \n" +
                "<RtCode/>     \n" +
                "<RtDesc/>     \n" +
                "<Backup1/>     \n" +
                "<Backup2/>     \n" +
                "<Backup3/>     \n" +
                "<Backup4/>       \n" +
                "<Backup5/>     \n" +
                "<Date>2023/03/08 08:33:00</Date>     \n" +
                "<Attrs>       \n" +
                "<Attr/>       \n" +
                "<Attr>\n" +
                "</Attr>     \n" +
                "</Attrs>   \n" +
                "</Head>   \n" +
                "</Msg>",url);
        System.out.println("result:"+s);
    }
    public static String sendWstXml(String xml,String url){
        // 拼接发送xml
        StringBuffer xmlStr = new StringBuffer();
        xmlStr.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:zy=\"http://tempuri.org/ZY_WS_MB_MSP\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <zy:req>\n" +
                "         <cString><![CDATA[");
        //动态参数
        xmlStr.append(xml);
        xmlStr.append( "]]></cString>\n" +
                "      </zy:req>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>");
        String body = HttpRequest.post(url)
                .setFollowRedirects(true)
                .setConnectionTimeout(15000)
                .setReadTimeout(15000)
                .contentType("text/xml;charset=UTF-8")
                .body(xmlStr.toString())
                .executeAsync().body();
        //响应xml 去除没用部分转换map
        String resultXml = body.replace("&lt;", "<").replace("&gt;", ">");
        resultXml = StringUtils.substringBetween(resultXml, "<cString>", "</cString>");
        return resultXml;
    }
    public static String sendWebService(String url, String operationName, String... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        Object[] objects;
        objects = client.invoke(operationName, params);
        return JSONObject.toJSONString(objects[0]);
    }
    public static String sendWebServiceByAxis(String url, String operationName, String... params) throws Exception {
        GetWSByAxis2 ws=new GetWSByAxis2();
        ws.WSUrl=url;
        Object result = ws.getStr(operationName, params);
        return JSONObject.toJSONString(result);
    }

}
